package otcv.hr.webapp.dao;

import java.sql.SQLException;
import java.util.List;

import otcv.hr.exception.CommonException;
import otcv.hr.log.Logger;
import otcv.hr.webapp.entity.UserEntity;
import otcv.hr.webapp.entity.UserSchEntity;

/**
 * @author : H-Thien
 * @comment : User Data Access Object
 */
public class UserDAO extends Dao {

	/**
	 * @comment : user list total count
	 */
	public String userTotalCnt(UserSchEntity sch) throws SQLException {
		String totCnt = "0";

		try {
			totCnt = (String) sqlMap.queryForObject("user.userTotalCnt", sch);
		} catch (SQLException e) {
			Logger.error("[UserDAO]userTotalCnt, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return totCnt;
	}

	/**
	 * @comment : user list
	 */
	public List<UserEntity> listUser(UserSchEntity sch) throws SQLException {
		List<UserEntity> list = null;
		try {
			list = sqlMap.queryForList("user.listUser", sch);
		} catch (SQLException e) {
			Logger.error("[UserDAO]listUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return list;
	}

	/**
	 * @comment : equal check
	 */
	public String equalCheckUser(UserEntity key) throws SQLException {
		String result = "0";

		try {
			result = (String) sqlMap.queryForObject("user.equalCheckUser", key);
		} catch (SQLException e) {
			Logger.error("[UserDAO]equalCheckUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return result;
	}

	/**
	 * @comment : user regist
	 */
	public void insertUser(UserEntity user) throws SQLException {
		try {
			sqlMap.insert("user.insertUser", user);
		} catch (SQLException e) {
			Logger.error("[UserDAO]insertUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}
	}

	/**
	 * @comment : user detail
	 */
	public UserEntity detailUser(UserEntity key) throws SQLException {
		UserEntity user = null;

		try {
			user = (UserEntity) sqlMap.queryForObject("user.detailUser", key);
		} catch (SQLException e) {
			Logger.error("[UserDAO]updateUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {
			sqlMap.endTransaction();
		}

		return user;
	}

	/**
	 * @comment : user update
	 */
	public int updateUser(UserEntity entity) throws SQLException, CommonException {
		int result = 0;
		try {
			result = sqlMap.update("user.updateUser", entity);
		} catch (SQLException e) {
			Logger.error("[UserDAO]updateUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return result;
	}

	/**
	 * @comment : user delete
	 */
	public int deleteUser(UserEntity key) throws SQLException, CommonException {
		int result = 0;
		try {
			result = sqlMap.delete("user.deleteUser", key);
			if (result != 1) {
				throw new CommonException("처리중 에러가 발생하였습니다.");
			}
		} catch (SQLException e) {
			Logger.error("[UserDAO]deleteUser, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return result;
	}

	/**
	 * @comment : user kind list
	 */
	public List<UserEntity> listUserUtil(String user_class) throws SQLException {
		List<UserEntity> list = null;
		try {
			list = sqlMap.queryForList("user.listUserUtil", user_class);
		} catch (SQLException e) {
			Logger.error("[UserDAO]listUserUtil, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return list;
	}

	/**
	 * @comment : get user name
	 */
	public String getUserName(UserEntity key) throws SQLException {
		String userName = "";
		try {
			userName = (String) sqlMap.queryForObject("user.getUserName", key);
		} catch (SQLException e) {
			Logger.error("[UserDAO]getUserName, SQLException:" + e);
			throw new SQLException(e.toString());
		} finally {

		}

		return userName;
	}
}